package com.gcgt.server.trader;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Market {
	
	private List<Trader> traders = new ArrayList<Trader>();
	
	private Map<String, MarketUpdate> updates = new HashMap<String, MarketUpdate>();
	
	public void addTrader(Trader trader) {
		traders.add(trader);
	}
	
	protected void notifyTraders(MarketUpdate update) {
		for (Trader trader : traders) {
			updates.put(update.getTicker(), update);
			trader.marketUpdate(this);
			
			System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(update.getDate()) + ";" + trader.getPortfolio().getValue(update) + ";" + trader.getCash() + ";" + trader.getPortfolio().getValue(update).add(trader.getCash()) + ";" + update.getOpen());
		}
	}

	public MarketUpdate getTicker(String ticker) {
		return updates.get(ticker);
	}
	
	public void sell(Trader trader, Position position, PayCallback callback) {
		
	}
	
	public void sell(Trader trader, Collection<Position> positions, PayCallback callback) {
		
	}
	
	public Position buy(Trader trader, String ticker, BigDecimal cash, PayCallback callback) {
//		BigDecimal availableCash = cash;
//		if (availableCash.subtract(getCommision()).compareTo(update.getOpen()) >= 0) {
//			Position position = Position.createPosition(update, availableCash);
//			
//			cash = cash.subtract(position.getTotalPrice().multiply(depositPercent));
//			cash = cash.subtract(commision);
//			portfolio.addPosition(position);
//		} 
		return null;
	}
	
	
}
